var time='';
var param='';
var current=1;
var jName = getCookie('Jname');
//我的权限数组
var arrayTitle = new Array; 
findMyCatalogue();
loadMyEssay(time, param);
//countPeopleMoney(gmtDatetime, name, phone,orderStatus);
var status = getvl("status");
var payType = getvl("payType");

$(function() {
	if(status==2 || status==4 || status==10){
		$("#a").attr("style","display:block;");
		$("#b").attr("style","display:none;");
		$("#c").attr("style","display:none;");
	}else if(status==3){
		$("#a").attr("style","display:none;");
		$("#b").attr("style","display:block;");
		$("#c").attr("style","display:none;");	
	}else if(status==7){
		$("#a").attr("style","display:none;");
		$("#b").attr("style","display:none;");
		$("#c").attr("style","display:block;");	
	}
});


function loadMyEssay(time, param) {
	$(document).ready(function() {
		init(current);
	});
	function init(pageNo) {
		$("#thislist").html("");
		$.ajax({
			url: urlcore + "/api/orders/selectListByPageAdmin?time=" + time + "&param=" + param +  "&current=" + pageNo+"&status=" + status+"&payType=" + payType,
			type: "get",
			async: 'false',
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.records, function(i, n) {
						var id = n.id;
						var time = '';
						if(n.status==2 || n.status==4 || n.status==10){
							time = formatDate(n.creatTime,"yyyy-MM-dd HH:mm:ss");
						}else if(n.status==3){
							time = formatDate(n.passTime,"yyyy-MM-dd HH:mm:ss");
						}else if(n.status==7){
							time = formatDate(n.finishTime,"yyyy-MM-dd HH:mm:ss");	
						}
						var isRemind = '';
						if(n.isRemind==0){
							isRemind ="否";	 
						}else if(n.isRemind==1){
							isRemind ="是";	 
						}
						var thislist =
							'<tr class="footable-even" style="display: table-row;">' +
							'<td class="footable-visible">' + n.id + '</td>' +
							'<td class="footable-visible">' + n.orderNumber + '</td>' +
							'<td class="footable-visible">' + n.user.realName + '</td>' +
							'<td class="footable-visible">' + n.user.phone + '</td>' +
							'<td class="footable-visible">' + n.product.productName + '</td>' +
							'<td class="footable-visible">' + n.price +'元' +'</td>' +	
							'<td class="footable-visible">' + n.number + '</td>' +
							'<td class="footable-visible">' + n.orderPrice +'元'+ '</td>' +
							'<td class="footable-visible">' + time  +'</td>' +
							'<td class="footable-visible">' + isRemind +'</td>' +
							'<td class="footable-visible footable-last-column">'+
								'<a hidden="hidden" class="" name="查看认证信息" href="personMessage.html?userId='+n.userId+'"  >查看认证信息</a>&nbsp;'+
								'<a hidden="hidden" class="" name="详情" href="cash_order_list_detail.html?id=' + n.id + '" >详情</a>&nbsp;';
								if(n.status==2){
									thislist += '<a hidden="hidden" name="发货" class="" href="javascript:;"  onclick="passCashOrder(' + id + ')">发货</a>&nbsp;'+
												'<a hidden="hidden" name="拒绝并退款" class="" href="javascript:;"  onclick="thisCannel(' + id + ')">取消退款</a>&nbsp;';
								}
								if(n.status==3){
									thislist += '<a hidden="hidden" name="确认收货" class="" href="javascript:;"  onclick="confirmCashOrder(' + id + ')">确认收货</a>&nbsp;';
								}
								'</td>'+			
							'</tr>';
					
						$('#thislist').append(thislist);

					});
					$.each(arrayTitle, function(i,k) {
						$('a[name="'+k+'"]').attr("hidden",false).attr("class","btn btn-primary btn-xs");
					});
					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});
				
					if(data.code == 'OVERTIME') {
						var thisUrl = window.location.href;

						if(thisUrl.indexOf('login.html') <= -1) {
							top.window.location.href = "login.html";
						}

					} else {
						if(data.msg != '空数据') {
							//alert(data.msg)
						} else {
							$('#thiscount').text(0);
						}
					}
				}

			},
			error: function() {
				alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}

}

function EnterSearchList() {
	var code = event.keyCode;
	if(code == 13) {
		searchList();
	}

}

function searchList() {
	var param = $('#param').val().trim();
	var time = $('#applyTime').val().trim();
	loadMyEssay(time, param);
	//countPeopleMoney(time, userName, phoneNumber,'');

}





function findMyCatalogue(){
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle="+jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				arrayTitle.push(n.thirdCatalogue.title);
			});
		} else {
			alert(data.msg);
		}

		},
		error:function() {
			alert("error");
		}
	});
}

/**
 * 时间格式化
 */
function formatDate(dateTime,format){
    return null==dateTime?"":new Date(dateTime).pattern(format);
}


function passCashOrder(id) {
	if(confirm("您确定发货？")) {
		$.ajax({
			url: urlcore + "/api/orders/passCashOrder?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				console.log(data.msg);
				alert(data.msg);
			}
		});
	}


}

function thisCannel(id) {
	if(confirm("您确定取消订单？您需要线下自行退款给用户")) {
		$.ajax({
			url: urlcore + "/api/orders/cancelOrder?id=" + id+"&type="+1,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				console.log(data.msg);
				alert(data.msg);
			}
		});
	}


}




function confirmCashOrder(id) {
	if(confirm("您确定已经收货？")) {
		$.ajax({
			url: urlcore + "/api/orders/confirmCashOrder?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				console.log(data.msg);
				alert(data.msg);
			}
		});
	}


}
